package handlers;

import influence_radii.Radial;
import item.Trap;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import map.GameMap;
import map.GameWorld;
import map.Position;
import entity.Entity;

public class DetectTrapHandler {
	private Entity detecter = null;
	
	public DetectTrapHandler(Entity detecter) {
		this.detecter = detecter;
	}
	
	public void detectTraps(int level) {
		int range = 3;
		GameMap map = GameWorld.getInstance().getCurrentMap();
		Radial radial = new Radial();
		List<Position> visiblePositions = new ArrayList<Position>();
		for(int index = 0; index <= range; index++) {
			List<Position> tempPositions = radial.getRelAppsPts(index);
			
			for(Position pos : tempPositions) {
				visiblePositions.add(pos);
			}
		}
		for(Position pos : visiblePositions) {
			int x = detecter.getPosition().getX();
			int y = detecter.getPosition().getY();
			visiblePositions.set(visiblePositions.indexOf(pos), new Position(pos.getX()+x, pos.getY()+y));
		}
		
		for(Position pos : visiblePositions) {
			if(map.hasTrap(pos)) {
				if(compute(level)) {
					Trap t = map.getTrap(pos);
					t.setVisible(true);
					System.out.println("trap is now visible");
				}
				else {
					System.out.println("failed to set trap visible");
				}
			}
		}
	}
	public boolean compute(int level) {
		level = level + 8;
		int max = 10;
		int[] values = new int[max];
		int result;
		Random ran = new Random();

		for(int i=1; i<values.length; i++)
			values[i-1] = i;
		
		result = (1 - (max - level)/(level))*10;
		if (result > max)
			result = max;
		int num = ran.nextInt(max) + 1;
		for(int i=0; i < result; i++) {
			if(values[i] == num)
				return true;
		}
		return false;
	}
	
}
